package src;

import src.config.SysConfig;
import src.server.AbstractServer;
import src.server.ScoreObj;
import src.server.SocketHelper;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.List;

/**
 * @author CsCN
 * 测试用的客户端
 */
public class Client {
    public void test() {
        try {
            System.out.println("client start");
            Socket socket = new Socket();
//            socket.setSoTimeout(10_000);
            socket.connect(new InetSocketAddress
                    ("127.0.0.1", SysConfig.SERVER_PORT), 5000);

            System.out.println("start battle");
            SocketHelper socketHelper = new SocketHelper(socket);
            socketHelper.sendString("matchH");
            socketHelper.sendString("CsCN");
            System.out.println(Thread.currentThread().getName() + socketHelper.recvString());
            System.out.println(Thread.currentThread().getName() + socketHelper.recvString());
            for (int i = 0; i < 21; i++) {
                Thread.sleep(1000);
                socketHelper.sendString(Integer.toString(i));
                System.out.println(Thread.currentThread().getName() + socketHelper.recvString());
            }
            socketHelper.sendString("GG");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void test1() {
        try {
            System.out.println("client start");
            Socket socket = new Socket();
//            socket.setSoTimeout(10_000);
            socket.connect(new InetSocketAddress
                    ("127.0.0.1", SysConfig.SERVER_PORT), 10000);

            System.out.println("start battle");
            SocketHelper socketHelper = new SocketHelper(socket);
//            Thread.sleep(1000);
            System.out.println("ready to macth");
            socketHelper.sendString("matchH");
//            System.out.println(socketHelper.recvString());
            socketHelper.sendString("Mashiro");
//            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + socketHelper.recvString());
            System.out.println(Thread.currentThread().getName() + socketHelper.recvString());
            for (int i = 0; i < 20; i++) {
                Thread.sleep(1000);
                socketHelper.sendString(Integer.toString(i * 10));
                System.out.println(Thread.currentThread().getName() + socketHelper.recvString());
            }
            socketHelper.sendString("GG");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void test2() {
        try {
            System.out.println("client start");
            Socket socket = new Socket();
            socket.setSoTimeout(10_000);
            socket.connect(new InetSocketAddress
                    ("127.0.0.1", SysConfig.SERVER_PORT), 5000);

            System.out.println("send requests");
            SocketHelper.sendString(socket, "getDataE");
            List<ScoreObj> scores = (List<ScoreObj>) SocketHelper.recvObject(socket);
            for (ScoreObj score : scores) {
                System.out.println(score.toString());
            }
        } catch (ClassCastException e) {
            e.printStackTrace();
            System.out.println("cast failed");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
